/**
 * 处理文件夹及文件-----拿到 locales/lang 中 各文件夹中的数据
 * @param {Object} modules js文件
 * @param {String} prefix  过滤的前缀路径
 */
export function genMessage(modules, prefix = "lang") {
	let fileModule = {};
	Object.keys(modules).forEach((file) => {
		let langFileModule = modules[file].default;
		let fileName = file.replace(`./${prefix}/`, "").replace(/^\.\//, ""); //正则删除 ./ 开头 和 zh-cn 或 en 开头的字符串
		const lastIndex = fileName.lastIndexOf(".");
		fileName = fileName.substring(0, lastIndex); //删除后缀
		const keyList = fileName.split("/");
		let prop = keyList.shift(); //拿到起始路径
		let obj = formatObj(keyList, langFileModule);
		fileModule[prop] = obj;
	});
	console.log(fileModule, "fileModule");
	return fileModule;
}
//处理数据
const formatObj = (arr, value) => {
	if (arr.length == 0) {
		return value;
	}
	let obj = {};
	let currentObj;
	arr.forEach((item, index) => {
		if (index != arr.length - 1) {
			if (currentObj) {
				currentObj[item] = {};
				currentObj = currentObj[item];
			} else {
				obj[item] = {};
				currentObj = obj[item];
			}
		} else {
			if (index == 0) {
				obj[item] = value;
			} else {
				currentObj[item] = value;
			}
		}
	});
	return obj;
};
